<?php
App::uses('AppController', 'Controller');
class CommentManagerController extends AppController{
    public $uses = array('Users','Comments');
    function beforeFilter(){
        $this->layout = 'admin';
    }
    function index(){
        $arrCondition = array();
        $cmd = @$this->request->query['cmd'];
        if($cmd == 'search'){
            $arrUserName = array();
            $arrUserNameComment = array();
            $userName    = @$this->request->query['u'];
            if($userName){
                $arrUserName['username LIKE']  =   '%'.$userName.'%';
                $this->set('user_name',$userName);
            }else{
                $this->set('user_name','');
            }
            $userComment = @$this->request->query['c'];
            if($userComment){
                $arrUserNameComment['username LIKE']  =   '%'.$userComment.'%';
                $this->set('user_comment',$userComment);
            }else{
                $this->set('user_comment','');
            }
            if(!empty($arrUserName)){
                $arrConditionUser['OR']    =   $arrUserName;
                $arrUserSearch = $this->Users->find('all',array('conditions' => $arrConditionUser));
                $arrUserId = array();
                if($arrUserSearch){
                    foreach($arrUserSearch AS $itemSearch){
                        $arrUserId[] = $itemSearch['Users']['id'];
                    }
                }
                if(!empty($arrUserId)){
                    $arrCondition['user_id'] = $arrUserId;
                }else{
                    $arrCondition = false;
                }
            }
            if(!empty($arrUserNameComment)){
                $arrConditionUser['OR']    =   $arrUserNameComment;
                $arrUserSearch = $this->Users->find('all',array('conditions' => $arrConditionUser));
                $arrUserId = array();
                if($arrUserSearch){
                    foreach($arrUserSearch AS $itemSearch){
                        $arrUserId[] = $itemSearch['Users']['id'];
                    }
                }
                if(!empty($arrUserId)){
                    $arrCondition['user_comment_id'] = $arrUserId;
                }else{
                    $arrCondition = false;
                }
            }
            $statsSearch = @$this->request->query['s'];
            if($statsSearch && $arrCondition !== false){
                if($statsSearch == 2){
                    $arrCondition['delete_flag']    =   0;
                }else{
                    $arrCondition['delete_flag']    =   1;
                }
            }
            $parentIdSearch = @$this->request->query['pid'];
            if($parentIdSearch){
                $this->set('parent_id',$parentIdSearch);
                if($arrCondition !== false)
                    $arrCondition['parent_id']    =   $parentIdSearch;
            }
        }elseif($cmd == 'del'){
            $idDel = $this->request->query['id'];
            $this->Comments->updateMutil(array(array('id' => $idDel,'delete_flag' => 1)));
            $this->redirect('/CommentManager/index');
        }
        $dataview = array();
        $arrUserInfo = array();
        $arrCommentLevel = $this->Comments->getDataByCondition('*',$arrCondition);
        if(!empty($arrCommentLevel)){
            $i = 0;
            $arrUserId = array();
            foreach($arrCommentLevel AS $itemComment){
                if(!in_array($itemComment['Comments']['user_id'],$arrUserId))
                    $arrUserId[] = $itemComment['Comments']['user_id'];
                if(!in_array($itemComment['Comments']['user_comment_id'],$arrUserId))
                    $arrUserId[] = $itemComment['Comments']['user_comment_id'];
                $dataview[] =   array(
                                'stt'               =>      ++$i,
                                'id'                =>      $itemComment['Comments']['id'],
                                'user_id'           =>      $itemComment['Comments']['user_id'],
                                'user_comment_id'   =>      $itemComment['Comments']['user_comment_id'],
                                'content'           =>      $itemComment['Comments']['comment'],
                                'create_time'       =>      $itemComment['Comments']['create_time'],
                                'update_time'       =>      $itemComment['Comments']['update_time'],
                                'parent_id'         =>      $itemComment['Comments']['parent_id'],
                                'delete_flag'       =>      $itemComment['Comments']['delete_flag'],
                                'stats'             =>      $itemComment['Comments']['delete_flag'] == 0 ? 'Hiển thị' : 'Đã xóa',
                                'level'             =>      $itemComment['Comments']['level']
                            );
            }
            if(!empty($arrUserId)){
                
                $arrUserGet = $this->Users->find('all',array('conditions' => array('id' => $arrUserId)));
                if($arrUserGet){
                    foreach($arrUserGet AS $itemUser){
                        $arrUserInfo[$itemUser['Users']['id']] =    $itemUser['Users']['username']; 
                    }
                }
            }
        }
        $this->set('dataview',$dataview);
        $this->set('arrUserInfo',$arrUserInfo);
    }
}
?>